#include "laurin-ip-range.h"


LaurinIPRange::LaurinIPRange(const LaurinIPRange& range)
{
	this->_start = range._start;
	this->_end = range._end;
	this->_nIPs = range._nIPs;
	this->reset();
}

LaurinIPRange* LaurinIPRange::getIPRange(u_int32_t networkAddress, u_int32_t subnetMask)
{
	LaurinIPRange* range = new LaurinIPRange();

	int iBits =  (0xffffffff ^ subnetMask);
	range->_end = networkAddress + iBits;
	range->_start = networkAddress + ntohl(1); 	//remove network address
	range->_nIPs = ntohl(iBits) - 1;			//remove broadcast

	range->reset();

	return range;
}


LaurinIPRange::LaurinIPRange(){}


u_int32_t LaurinIPRange::getCurrentIPAddress() const
{
	return _curIP.s_addr;
}


u_int32_t LaurinIPRange::getNextIPAddress()
{
	next();
	return getCurrentIPAddress();
}


void LaurinIPRange::next()
{
	if(_curOffset >= _nIPs)
		_curOffset = -1;

	_curOffset++;
	_curIP.s_addr = _start + ntohl(_curOffset);
}


void LaurinIPRange::reset()
{
	_curOffset = 0;
	_curIP.s_addr = _start;
}


u_int32_t LaurinIPRange::getFirstIPAddress() const
{
	return _start;
}


u_int32_t LaurinIPRange::getLastIPAddress() const
{
	return _end;
}


int LaurinIPRange::getNumbersOfIPAddress() const
{
	return _nIPs;
}

